import sys

if len(sys.argv) < 3:
    print 'Error: Workload file or vid file not specified.'
    sys.exit(1)
flows = []

with open(sys.argv[1], 'r') as fin:
    line = fin.readline().strip() # skip the first row
    line = fin.readline().strip()
    while line != '':
        tokens = line.split(' ')
        flows.append([tokens[0].split(':')[1], tokens[1].split(':')[1]])
        line = fin.readline().strip()
pid2vid = {}
vid2pid = {}
with open(sys.argv[2], 'r') as fin:
    line = fin.readline().strip()
    while line != '':
        tokens = line.split(' ')
        pid = tokens[0].split(':')[1]
        vid = tokens[1]
        pid2vid[pid] = vid
        vid2pid[vid] = pid
        line = fin.readline().strip()

total_packets_sent = 0
total_packets_received = 0

for flow in flows:
    packets_sent = 0
    packets_received = 0
    with open(flow[0] + '.stats') as source_in:
        line = source_in.readline().strip()
        while line != '':
            tokens = line.split(' ')
            if len(tokens) < 5:
                print 'Error: unexpected format in stats file'
            if tokens[0] == 'Sending' and tokens[4] == pid2vid[flow[1]]:
                packets_sent += 1
            line = source_in.readline().strip()
    with open(flow[1] + '.stats') as dest_in:
        line = dest_in.readline().strip()
        while line != '':
            tokens = line.split(' ')
            if len(tokens) < 5:
                print 'Error: unexpected format in stats file'
            if tokens[0] == 'Received' and tokens[4] == pid2vid[flow[0]]:
                packets_received += 1
            line = dest_in.readline().strip()
    if packets_sent == 0:
        print 'Error: flow from', flow[0], 'to', flow[1], 'was never started'
    else:
        print packets_received, 'of', packets_sent, 'packets transmitted from', flow[0], 'to', flow[1], '(', 100*packets_received/packets_sent, '%)'
    total_packets_sent += packets_sent
    total_packets_received += packets_received

packets_dropped_by_reason = {'no route':0, 'ttl':0, 'unknown':0}
packets_dropped_by_node = {}
node_loads = {}
for pid in pid2vid:
    packets_dropped_by_node[pid] = 0
    node_loads[pid] = {'Sent': 0, 'Received': 0, 'Routed': 0}
    with open(pid + '.stats') as node_in:
        line = node_in.readline().strip()
        while line != '':
            tokens = line.split(' ')
            if tokens[0] == 'Dropped' and tokens[1] == 'packet:':
                packets_dropped_by_node[pid] += 1
                if tokens[2] == 'no' and tokens[3] == 'route':
                    packets_dropped_by_reason['no route'] += 1
                elif tokens[2] == 'TTL' and tokens[3] == 'exceeded':
                    packets_dropped_by_reason['ttl'] += 1
            elif tokens[0] == 'Sending':
                node_loads[pid]['Sent'] += 1
            elif tokens[0] == 'Received':
                node_loads[pid]['Received'] += 1
            elif tokens[0] == 'Routing':
                node_loads[pid]['Routed'] += 1
            line = node_in.readline().strip()
print total_packets_sent, 'data packets sent,', total_packets_received, 'received'
total_packets_dropped = total_packets_sent - total_packets_received
packets_dropped_by_reason['unknown'] = total_packets_dropped - packets_dropped_by_reason['no route'] - packets_dropped_by_reason['ttl']
print 'Packets dropped by reason:', packets_dropped_by_reason
print 'Packets dropped by node:'
for pid in packets_dropped_by_node:
    print '\tNode', pid, ':', packets_dropped_by_node[pid]
print 'Loads on each node:'
for pid in node_loads:
    print '\tNode', pid, ':', node_loads[pid]
